package com.tisson.bo;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.tisson.dao.AcDepartmentDAO;
import com.tisson.dao.AdminAreaDAO;
import com.tisson.dao.AdminDAO;
import com.tisson.dao.AdminDepartDAO;
import com.tisson.dao.AdminRightDAO;
import com.tisson.dao.AreaDAO;
import com.tisson.dao.FunctionDAO;
import com.tisson.pojo.AcDepartment;
import com.tisson.pojo.Admin;
import com.tisson.pojo.AdminArea;
import com.tisson.pojo.AdminDepart;
import com.tisson.pojo.AdminRight;
import com.tisson.pojo.Area;
import com.tisson.pojo.Function;

public class LoginBO {
	
	public void saveUserInfo(HttpServletRequest request,String username,String password){
		try{
			AdminDAO adminDAO = new AdminDAO();
			AdminDepartDAO adminDepartDAO = new AdminDepartDAO();
			AdminAreaDAO adminAreaDAO = new AdminAreaDAO();
			AcDepartmentDAO acDepartmentDAO = new AcDepartmentDAO();
			AreaDAO areaDAO = new AreaDAO();
			Admin admin = new Admin();
			List adminDeptList = new ArrayList();
			List adminAreaList = new ArrayList();
			List deptList;
			List areaList;
			List list = adminDAO.findAll();
			for (Iterator iterator = (Iterator) list.iterator(); iterator.hasNext();) {
				Admin adminTemp = (Admin) iterator.next();
				if (username.equals(adminTemp.getUsername())) {
					admin.setId(adminTemp.getId());
					admin.setName(adminTemp.getName());
					admin.setPassword(adminTemp.getPassword());
					admin.setRole(adminTemp.getRole());
					admin.setTel(adminTemp.getTel());
					admin.setUsername(adminTemp.getUsername());
					areaList = adminAreaDAO.findByAdminId(admin.getId());
					deptList = adminDepartDAO.findByAdminId(admin.getId());
					for(int i=0;i<deptList.size();i++){
						adminDeptList.add(acDepartmentDAO.findById(((AdminDepart)deptList.get(i)).getDepartId()));
					}
					for(int i=0;i<areaList.size();i++){
						adminAreaList.add(areaDAO.findById(((AdminArea)areaList.get(i)).getAreaId()));
					}
					request.getSession().setAttribute("admin", admin);
					request.getSession().setAttribute("adminDeptList", adminDeptList);
					request.getSession().setAttribute("adminAreaList", adminAreaList);
				}
			}	
		}catch(Exception ex){
			ex.printStackTrace();
			HttpSession session = request.getSession();
			Enumeration e = session.getAttributeNames();
			while (e.hasMoreElements()) {
				String sessionName = (String) e.nextElement();
				session.removeAttribute(sessionName);
			}
		}
	}
	
	public List getMenu(HttpServletRequest request){
		FunctionDAO functionDAO = new FunctionDAO();
		AdminRightDAO adminRightDAO = new AdminRightDAO();
		try{
			List rightList = adminRightDAO.findByAdminId(((Admin)request.getSession().getAttribute("admin")).getId());
			List funList = functionDAO.findAll();
			List menu = new ArrayList();
			for(int j=0;j<rightList.size();j++){
				Function fun = new Function();
				Function temp =functionDAO.findById(((AdminRight)rightList.get(j)).getFuncId());
				fun.setId(temp.getId());
				fun.setName(temp.getName());
				fun.setUrl(temp.getUrl());
				fun.setNode(temp.getNode());
				if(fun!=null){
					menu.add(fun);
				}
			}
			return menu;
		}catch(Exception e){
			return new ArrayList();
		}
	}

}
